<?php
namespace Common\Model;

/**
 * 区资源表
 * @modify trainyao 2015-12-31 创建
 * @index district(`city_id`)
 */

Class DistrictModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_district';
    
    /**
     * 返回区域名称
     * @param integer $districtId  区域ID
     * @return type
     */
    public function getName($districtId) {
        return $this->where(array('id' => $districtId))->getField('name');
    }
    
    /**
     * 获取区数据
     * @modify trainyao 2015-12-31  创建
     * @modify trainyao 2016-01-09  支持获取多个id
     * @param  integer   $id 区id
     * @return array
     */
    public function getInfo($id = null) {
        $option = array();
        if (!empty($id)) {
            if (is_array($id)) {
                $id = implode(',', $id);
            }
            if (strpos($id, ',') !== false) {
                $option['id'] = array('in', $id);
            } else {
                $option['id'] = $id;
            }
        }
        $option['isdelete'] = NOT_DELETE;
        
        $fields = 'id,city_id,name';
        $data = array();
        
        if (!empty($id)) {
            if (strpos($id, ',') !== false) {
                $data = $this->where($option)->field($fields)->select();
            } else {
                $data = $this->where($option)->field($fields)->find();
            }
        }
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 根据城市取区
     * @modify trainyao 2015-12-31 创建
     * @param  integer   $cityId 城市id
     * @return array
     */
    public function getInfoByCity($cityId) {
        $option = array('city_id' => $cityId, 'isdelete' => NOT_DELETE);
        $fields = 'id,city_id,name';
        
        $data = $this->where($option)->field($fields)->select();
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 增加区
     * @modify trainyao 2015-12-31 创建
     * @param  string   $cityId      城市id
     * @param  string   $name      区名
     * @return boolean 执行结果
     */
    public function addInfo($cityId, $name) {
        $data = array('city_id' => $cityId, 'name' => $name);
        return $this->add($data) === false ? false : true;
    }
}
